
import java.net.HttpURLConnection;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.HashMap;



public class netutils
{
	//编码
	private static String charsetname = "UTF-8";
	//超时
	private static int Timeout = 5000;
	//user-agent
	private static String[] UAS = {
		"Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0"/*Windows*/,
		"Mozilla/5.0 (Linux; Android 9; ONEPLUS A6000) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.99 Mobile Safari/537.36"/*Ardnoid9*/
	};
	private static String UA = UAS[0];
	public static int PC = 0;
	public static int Android = 1;

	//cookie
	private static String cookie = "";
	private static String Setcookie = "";
	//headers
	private static Map<String,String> Headers = new HashMap<String,String>();
	//返回头
	private static Map<String,List<String>> Setheaders;

	/*设置全局编码*/
	public static void setcharsetname(String charset){
		charsetname = charset;
	}
	/*设置全局超时*/
	public static void setTimeout(int timeout){
		Timeout = timeout;
	}
	/*设置User-Agent*/
	public static void setUserAgent(int ua){
		UA = UAS[ua];
	}
	/*设置请求头*/
	public static void setHeader(Map<String,String> headers){
		Headers = headers;
	}
	/*获取上次请求的返回头*/
	public static Map<String,List<String>> getHeaders(){
		return Setheaders;
	}
	/*设置请求cookie*/
	public static void setCookie(String ck){
		cookie = ck;
	}
	/*获取上次请求返回的cookie*/
	public static String getCookie(){
		return Setcookie;
	}
	/*http get*/
	public static String _GET(String URl) {
		try {
			int i;
			HttpURLConnection conn = (HttpURLConnection) new URL(URl).openConnection();
			conn.setRequestMethod("GET");
			conn.setConnectTimeout(Timeout);
			conn.setReadTimeout(Timeout);
			if(Headers.size() >= 1){
				for(String key : Headers.keySet()){
					conn.setRequestProperty(key,Headers.get(key));
				}
			}
			conn.setInstanceFollowRedirects(true);//允许重定向
			conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml,*/*");
			conn.setRequestProperty("User-Agent",UA);
			conn.setRequestProperty("Accept-Language","zh-CN,zh-TW,zh-HK,en-US");
			//conn.setRequestProperty("Accept-Encoding","gzip,deflate,br");
			conn.setRequestProperty("Connection","keep-alive");
			if (cookie != null && !cookie.equals("")) {
				conn.setRequestProperty("Cookie", cookie);
			}
			InputStream inStream = conn.getInputStream();
			ByteArrayOutputStream outStream = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024];
			while (true) {
				i = inStream.read(buffer);
				if (i == -1) {
					break;
				}
				outStream.write(buffer, 0, i);
			}
			String str = new String(outStream.toByteArray(),charsetname);
			Setcookie = "";
			if (conn != null) {
				String cookieVal = null;
				String key=null;
				for (int j = 1; (key = conn.getHeaderFieldKey(j)) != null; j++ ) {
					if (key.equalsIgnoreCase("set-cookie")) {
						cookieVal = conn.getHeaderField(j);
						cookieVal = cookieVal.substring(0, cookieVal.indexOf(";"));
						Setcookie = Setcookie+cookieVal+";";
					}
				}
				Setheaders = conn.getHeaderFields();
			}
			outStream.close();
			inStream.close();
			return str;
		} catch (Exception e) {
			e.printStackTrace();
			return e.toString();
		}
	}
	public static String _POST(String URl, String data) {
		String result = "";
		Exception e;
		try {
			HttpURLConnection conn = (HttpURLConnection) new URL(URl).openConnection();
			conn.setRequestMethod("POST");
			conn.setReadTimeout(Timeout);
			conn.setConnectTimeout(Timeout);
			if(Headers.size() >= 1){
				for(String key : Headers.keySet()){
					conn.setRequestProperty(key,Headers.get(key));
				}
			}
			conn.setRequestProperty("Accept", "application/json,text/plain,*/*");
			conn.setRequestProperty("User-Agent",UA);
			//conn.setRequestProperty("Accept-Language","zh-CN,zh-TW,zh-HK,en-US");
			//urlConnection.setRequestProperty("Accept-Encoding","gzip");
			//conn.setRequestProperty("Connection", "keep-alive");
			conn.setRequestProperty("Content-Type", "application/json,charset=UTF-8");
			conn.setRequestProperty("Content-Length", String.valueOf(data.getBytes().length));

			if (cookie != null && !cookie.equals("")) {
				conn.setRequestProperty("Cookie", cookie);
			}
			conn.setDoOutput(true);
			conn.setDoInput(true);
			OutputStream os = conn.getOutputStream();
			os.write(data.getBytes(charsetname));
			os.flush();
			InputStream is = conn.getInputStream();
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024];
			while (true) {
				int len = is.read(buffer);
				if (len == -1) {
					break;
				}
				baos.write(buffer, 0, len);
			}
			is.close();
			baos.close();
			String str = new String(baos.toByteArray(),charsetname);
			try {
				Setcookie = "";
				if (conn != null) {
					String cookieVal = null;
					String key=null;
					for (int j = 1; (key = conn.getHeaderFieldKey(j)) != null; j++ ) {
						if (key.equalsIgnoreCase("set-cookie")) {
							cookieVal = conn.getHeaderField(j);
							cookieVal = cookieVal.substring(0, cookieVal.indexOf(";"));
							Setcookie=Setcookie+cookieVal+";";
						}
					}
				}

				return str;
			} catch (Exception e2) {
				e = e2;
				result = str;
				e.printStackTrace();
				return result;
			}
		} catch (Exception e3) {
			e = e3;
			e.printStackTrace();
			return result;
		}
	}
}
